﻿using System;
using System.Web.UI.WebControls;
using SalePartial.BLL;
using XunPan.Web;
using SalePartial.Utility;
using System.Data;
using Telerik.Web.UI;
using System.Web.UI;

public partial class ManageRoles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ckint = 0;

            //DateTime ckbtime, cketime;

            // 检查用户权限并存入使用记录。
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type != LoginUserType.ADMIN)
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);
            }

           //DataSet ds = UserBLL.Select_Role( null, null, null);
           //gv_User.DataSource = ds;
           //gv_User.DataBind();
            
        }
        
    }

    private void initData()
    {
        cbIsLockOut.Checked = false;
        sqlDSUser.SelectParameters["isLocked"].DefaultValue = "0";
    }

    // Check_Power() 检查用户权限
    private void check_power(string f_power)
    {
        // 载入公共函数

        // 若 Session 不存在则直接显示错误消息
        try
        {

            if (SecurityUtility.Check_Power(Session["UserName"].ToString(), Session["mg_power"].ToString(), f_power) > 0)
                Response.Redirect("../Error.aspx?ErrCode=1");
        }
        catch
        {
            Response.Redirect("../Error.aspx?ErrCode=2");
        }
    }

    protected void gv_User_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        //lb_pageid.Text = gv_User.PageIndex.ToString();
    }

    protected void Btn_Set_Click(object sender, EventArgs e)
    {
        sqlDSUser.SelectParameters["isLocked"].DefaultValue = cbIsLockOut.Checked.ToString();
        sqlDSUser.SelectParameters["UserName"].DefaultValue =
            tbUserName.Text.Trim() == string.Empty ? "%" : tbUserName.Text.Trim();
        sqlDSUser.SelectParameters["createStDay"].DefaultValue =
            dpCreateStDay.SelectedDate == null ? "19000101" : string.Format("{0:yyyyMMdd}", dpCreateStDay.SelectedDate);
        sqlDSUser.SelectParameters["createEdDay"].DefaultValue =
            dpCreateEdDay.SelectedDate == null ? "20991231" : string.Format("{0:yyyyMMdd}", dpCreateEdDay.SelectedDate);
        sqlDSUser.SelectParameters["lastLoginStDay"].DefaultValue =
            dpLastLoginStDay.SelectedDate == null ? "19000101" : string.Format("{0:yyyyMMdd}", dpLastLoginStDay.SelectedDate);
        sqlDSUser.SelectParameters["lastLoginEdDay"].DefaultValue =
            dpLastLoginEdDay.SelectedDate == null ? "20991231" : string.Format("{0:yyyyMMdd}", dpLastLoginEdDay.SelectedDate);


        gv_User.DataBind();
    }

    protected void gv_User_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        GridEditManager editMan = editedItem.EditManager;

        RadTextBox rtbRoleNo = (RadTextBox)editMan.GetColumnEditor("mg_id").ContainerControl.FindControl("rtbRoleNo");
        RadTextBox rtbRoleName = (RadTextBox)editMan.GetColumnEditor("RoleName").ContainerControl.FindControl("rtbRoleName");
        RadTextBox rtbDescription = (RadTextBox)editMan.GetColumnEditor("Description").ContainerControl.FindControl("rtbDescription");

        DataSet ds = UserBLL.Select_Role(null, rtbRoleName.Text, null);

        if (ds != null && ds.Tables != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            gv_User.Controls.Add(new LiteralControl("此‘角色名称’已经使用，请重新设置!"));
            return;
        }

        UserBLL.UpdateRole(rtbRoleNo.Text.Trim(),rtbRoleName.Text.Trim(), rtbDescription.Text.Trim(), "");
    }


    protected void gv_User_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

    }

    protected void gv_User_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

    }

    protected void gv_User_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "approve":
               Guid userid = (Guid)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserId"];
                GridDataItem dataItem = (GridDataItem)e.Item;
                CheckBox checkbx = (CheckBox)dataItem["IsLockedOut"].FindControl("cbIsLockedOut");
                bool isLockedOut = checkbx.Checked;
                checkbx = (CheckBox)dataItem["IsApproved"].FindControl("cbIsApproved");
                bool isApproved = checkbx.Checked;
                bool rt = UserBLL.UpdateUser(userid, isLockedOut, isApproved);
                if (rt)
                {
                    //success
                    e.Item.OwnerTableView.Rebind();

                }
                else
                {
                }
                break;
        }

    }
    //protected void gv_User_ItemDataBound(object sender, GridItemEventArgs e)
    //{

    //    if (e.Item is GridDataItem)
    //    {
    //        GridDataItem dataItem = (GridDataItem)e.Item;
    //        CheckBox checkbx = (CheckBox)dataItem["IsLockedOut"].Controls[0];
    //        dataItem.Attributes.Add("ondblclick", "SelectCheckBox('" + dataItem.ItemIndex + "','" + checkbx.ClientID + "');");
    //    }

    //}
}
